﻿using System;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using System.IO;

namespace AdventureWorksLT.Helpers
{
    public class ImageConverter : IValueConverter
    {

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            byte[] bytes = value as byte[];
            string byteString;
            if (value is string)
            {
                byteString = value.ToString();
                string[] strings = byteString.Split(",".ToCharArray());
                bytes = new byte[strings.Length];
                for (int i = 0; i < strings.Length; i++)
                {
                    bytes[i] = Byte.Parse(strings[i]);
                }
            }
            if (bytes != null)
            {
                BitmapImage bitmap = new BitmapImage();
                MemoryStream s = new MemoryStream(bytes);
                bitmap.SetSource(s);
                return bitmap;
            }
            else
                return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
